package com.chapter.sample.jvm.gc;

import java.util.concurrent.TimeUnit;

/**
 * 引用计数
 */
public class RefCountGC {

    //定义成员变得占用内存空间
    private byte[] bigSize = new byte[5 * 1024 * 1024];

    Object reference = null;

    public static void main(String[] args) {
        RefCountGC obj1 = new RefCountGC();
        RefCountGC obj2 = new RefCountGC();

        obj1.reference = obj2;
        obj2.reference = obj1;

        obj1 = null;
        obj2 = null;

        //显示执行垃圾回收
        //发送GC，obj1 和 obj2 能否回收？
        System.gc();

        try {
            TimeUnit.SECONDS.sleep(10);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

        }
    }
}
